<?php

class LinksController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/admin/main';

	public function init(){
        Yii::app()->user->loginUrl = array('/admin/site/login');
        parent::init();
    }

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('login','logout'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update', 'index', 'view', 'new', 'edit'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionNew()
	{
		$members = Members::model()->findAll();
		$this->render('new',array(
			'members' => $members,
		));
	}

	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	public function actionCreate()
	{
		if($_POST['link'])
		{
			$model=new Links;
			$model->attributes=$_POST['link'];
      $model->modified_at = date('Y-m-d H:i:s');
			if($model->save())
			{
				$this->redirect(array('index'));
			}
			else
			{
				$members = Members::model()->findAll();
				$this->render('new',array(
					'link'=>$model,
					'members'=>$members,
				));
			}
		}
	}

	public function actionEdit()
	{
		$members = Members::model()->findAll();
		$link = Links::model()->find('id = :id',array(':id' => $_GET['id']));
		$this->render('edit',array(
			'link'=>$link,
			'members'=>$members,
		));
	}

	public function actionUpdate()
	{
		if(isset($_POST['link']))
		{
			$data=$_POST['link'];
			$link=Links::model()->find('id = :id',array(':id' => $data['id']));
			$link->attributes=$data;
      $link->modified_at = date('Y-m-d H:i:s');
			if($link->save())
			{
				$this->redirect(array('index'));
			}

			$members = Members::model()->findAll();
			$this->render('edit',array(
				'link'=>$link,
				'members'=>$members,
			));
		}
	}

	public function actionDelete()
	{
		$link=Links::model()->find('id = :id', array(':id'=>$_GET['id']));
		$link->delete();
		$this->redirect(bu('admin/links/'));
	}

	public function actionIndex()
	{
		$links=Links::model()->with('member')->findAll();
		$this->render('index',array(
			'links' => $links,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Links::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='links-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
